/*
 * TestServer.java
 *
 * Created on July 30, 2007, 10:03 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.google.projects.chai.testutil;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.servlet.ServletException;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.ServletWebServer;
import org.apache.xmlrpc.webserver.XmlRpcServlet;

/**
 * XML-RPC server for testing.
 * @author dap
 */
public class TestServer {
        
        private static final int port = 8080;
        private final ServletWebServer server;
        
        /**
         * Creates a new instance of TestServer
         */
        public TestServer() throws ServletException {
                XmlRpcServlet servlet = new XmlRpcServlet() {
                        protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws XmlRpcException {
                                PropertyHandlerMapping phm = new PropertyHandlerMapping();
                                phm.addHandler("User", 
                                    com.google.projects.chai.xmlrpc.UserHandler.class);
                                return phm;
                        }
                };
                
                server = new ServletWebServer(servlet, port);
                XmlRpcServerConfigImpl cfg = (XmlRpcServerConfigImpl) 
                        server.getXmlRpcServer().getConfig();
        }
        
        public int getPort() {
                return port;
        }
        
        public void start() throws IOException {
                server.start();
        }
        
        public void shutdown() {
                Thread.yield(); /* Otherwise, shutdown hangs */
                server.shutdown();
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws Exception {
                new TestServer().start();
        }
}
